#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <fstream>
#include <QDir>
#include <QFileDialog>
#include <functional>
#include <semaphore.h>
#include "viewer_2d_widget.h"
#include "common/common.h"
#include "common/utils.h"
#include <unistd.h>
#include "data_provider/data_provider.h"
#include "communication/communication.h"
#include "dock/wps_process_dock.h"
#include "common/utils.h"

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

public:
    int traking_gps_flag_ = 0;
    QLabel *locationLabel;
    QLabel *mouseLable;
    QLabel *timeLable;


 private:
    Viewer2DWidget *map_widget_;
    QMenuBar *menuBar;
    QToolBar *editToolBar;
    QMenu *menuwindow;
    QMenu *menu_mode_;
    WPSProcessDock *wps_process_dock_ptr_;
    QTimer *timer1;
    QAction *find_car_;
    QAction *record_gps_;
    QAction *record_wps_;
    QAction *edit_wps_;
    QAction *edit_gps_;
    QAction *traking_gps_;
    QAction *measure_point_;
    QAction *open_map_;
    QAction *single_action;
    QAction *circle_action;
    QAction *multiple_action;
    ConfigInfo config_info_;
    STRoadPoint ego_position_;
    STRoadPoint base_position_;
    std::thread *record_gps_thread_ = nullptr;
    std::shared_ptr<std::thread> subscribe_topics_thread = nullptr;
    std::shared_ptr<std::thread> publish_topics_thread = nullptr;

    std::ofstream gps_log_file_;
    std::ofstream wps_log_file_;
    int gps_file_num_ = 10;
    sem_t wps_semaphore_;
    bool first_time_write_ = false;
    QString operate_selected_directory_;
    std::vector<STRoadPoint> write_gps_points_;
    std::vector<STRoadPoint> write_wps_points_;
    bool initialization_ = false;

public:
    std::vector<STRoadPoint> RecordWPSPointsInfo(){return write_wps_points_;}
    std::vector<STRoadPoint> RecordGPSPointsInfo(){return write_gps_points_;}


private:
    void creat_menu_bar();
    void creat_tools_bar();
    void creat_status_bar();
    void find_car_tool_triggered();
    void RecordGPSProcessThread(const std::string &dir);
    void UpdateActionChecked();
    void WriteWPSOrGPSToFile(std::ofstream *wps_file,std::ofstream *gps_file,const STRoadPoint point,bool write_wps,bool write_gps,int &point_state,int &wps_seqnum);

private slots:
    void loop();
    void RecordGPSPointsTriggered();
    void RecordWPSPointsTriggered();
    void OpenMapFileTriggered();
    void EditWPSPointsTriggered();
    void EditGPSPointsTriggered();
    void TrakingGPSPointsTriggered();
    void SaveMapInfoToMapFileTriggered();
    void TrakingModeTriggered();
    void MeasurementPointsTriggered();


signals:
    void OpenMapFileListSignal(const std::string &folder,std::vector<std::string> &file_lists);

};
#endif // MAINWINDOW_H
